# -*- coding:utf-8 -*-
import requests
import json
import time
import random


def yk_comment_spilder(page=0, iid=0):
    """爬取弹幕评论内容"""
    url = "https://service.danmu.youku.com/list" \
          "?jsoncallback=jQuery1112007347297978278422_1571145834837" \
          "&mat=%s&mcount=1&ct=1001&iid=%s&aid=318071&cid=97" \
          "&lid=0&ouid=0&_=1571145834853" % (page, iid)
    request_headers = {"Referer": "https://v.youku.com/v_show/id_XMjk4ODAyMzIyOA==.html"
                                  "?spm=a2h0k.11417342.soresults.dselectbutton&s=efbfbd2ec49c46efbfbd",
                       "User-Agent": "Mozilla/5.0"}

    try:
        res = requests.get(url, headers=request_headers)
        res.raise_for_status()
        comment = res.text[res.text.index("{"):-2]
        # print(comment)
        return comment
    except:
        print('爬取第' + str(page) + '页失败')


def yk_vid_spilder():
    """爬取页码"""
    global res
    url = "https://acs.youku.com/h5/mtop.youku.play.ups.appinfo.get/1.1/?jsv=2.4.16&appKey=24679788&t=1571206489734&sign=008bcc26c8c2d74750e26b013059e627&api=mtop.youku.play.ups.appinfo.get&v=1.1&timeout=20000&YKPid=20160317PLF000211&YKLoginRequest=true&AntiFlood=true&AntiCreep=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=%7B%22steal_params%22%3A%22%7B%5C%22ccode%5C%22%3A%5C%220502%5C%22%2C%5C%22client_ip%5C%22%3A%5C%22192.168.1.1%5C%22%2C%5C%22utid%5C%22%3A%5C%22jRcZFaovJy0CAXd74ttufVC3%5C%22%2C%5C%22client_ts%5C%22%3A1571206489%2C%5C%22version%5C%22%3A%5C%221.9.6%5C%22%2C%5C%22ckey%5C%22%3A%5C%22121%23U7wlkzEtJYMlVl1F%2B8HZllXYecxDK3jVZGccqGv2iwZrOs%2FVTuDkl9jYOc8fD7jVlGgY%2BaPIKMlVAQrrEmDIlwRITn3%2BxujVlmMY%2BzPIpMmlOQrJEmD5lwLYAcfdK7jVlGgY%2BaP5KMlVA3rnEm65lw9Y1vJBk4QmltXgebCD8qSm%2F0bvGV5tCbibYQhEU960C6WinnCVp2D0CZ0T8uBhbZsbkeIaF9QVVnV76q29pXseC60T8uVICbibCeIaFtFbbZsbnjxVp2D0CZ0T83BhbZs0CehiFtK0gZXgNnx9on1zUr7N2uO4bJ1z5%2ByaFw1blPsbnj%2BalQSNOMVRD%2Bq0ifpQbnV0vYA%2Btutt2NEcvETWr7iyw1aO6v9UrgvG3bF8xrUvud%2FRkNdtXYYpVOWDiriQYHYDWqsfm%2BcfxaONNMXCgOLzqlDGj7THsELr8ocKeBx90r7bSjBTwf8qA1VH1hojS5qRiFke8MAr8KRGTcrUvAkVJ2VV6WsYIdQmrFr0WV26FLuf0S79jnct4McZUMY5iZ7UiHxot0Cgv%2BrOscmufHL6iWNkih%2BgQO%2BFHyKwygOiZHY6YyNeNyVo2FoSMI%2FxbU7Kj8FMbrwUVdxrKAWVDz4ofp7egCuupOR3HTOMRhGlaXyGWBRBEkHZIaVLB6wmnjSJGDer58LwJj8f%2B53Zy6ydF0EBiLnDZXxiC9xImaxDx9nYZcAbLjH0zY%2BrUs1TZmYZU69i6zvSWYLYesgl0xyGnwTdLgzQNlnguPe4Hx8lAYXUBvs%2FixdQyuUKLuaMz98wrp70yLKWodYQNPzVHvJi%2Fopo03cMQ8g9p992jt%2FZd98p5WWMBdKT%2Bw%3D%3D%5C%22%7D%22%2C%22biz_params%22%3A%22%7B%5C%22vid%5C%22%3A%5C%22XMjk4ODAyMzIyOA%3D%3D%5C%22%2C%5C%22play_ability%5C%22%3A5376%2C%5C%22current_showid%5C%22%3A%5C%22318071%5C%22%2C%5C%22preferClarity%5C%22%3A3%2C%5C%22master_m3u8%5C%22%3A1%2C%5C%22media_type%5C%22%3A%5C%22standard%2Csubtitle%5C%22%2C%5C%22app_ver%5C%22%3A%5C%221.9.6%5C%22%7D%22%2C%22ad_params%22%3A%22%7B%5C%22vs%5C%22%3A%5C%221.0%5C%22%2C%5C%22pver%5C%22%3A%5C%221.9.6%5C%22%2C%5C%22sver%5C%22%3A%5C%222.0%5C%22%2C%5C%22site%5C%22%3A1%2C%5C%22aw%5C%22%3A%5C%22w%5C%22%2C%5C%22fu%5C%22%3A0%2C%5C%22d%5C%22%3A%5C%220%5C%22%2C%5C%22bt%5C%22%3A%5C%22pc%5C%22%2C%5C%22os%5C%22%3A%5C%22win%5C%22%2C%5C%22osv%5C%22%3A%5C%227%5C%22%2C%5C%22dq%5C%22%3A%5C%22auto%5C%22%2C%5C%22atm%5C%22%3A%5C%22%5C%22%2C%5C%22partnerid%5C%22%3A%5C%22null%5C%22%2C%5C%22wintype%5C%22%3A%5C%22interior%5C%22%2C%5C%22isvert%5C%22%3A0%2C%5C%22vip%5C%22%3A0%2C%5C%22emb%5C%22%3A%5C%22Ajc0NzAwNTgwNwJ2LnlvdWt1LmNvbQIvdl9zaG93L2lkX1hNams0T0RBeU16SXlPQT09Lmh0bWw%3D%5C%22%2C%5C%22p%5C%22%3A1%2C%5C%22rst%5C%22%3A%5C%22mp4%5C%22%2C%5C%22needbf%5C%22%3A2%7D%22%7D"
    request_headers = {
        "Referer": "https://v.youku.com/v_show/id_XMjk4ODAyMzIyOA==.html?tpa=dW5pb25faWQ9MTAzNzUzXzEwMDAwMV8wMV8wMQ&refer=sousuotoufang_market.qrwang_00002944_000000_QJFFvi_19031900",
        "User-Agent": "Mozilla/5.0",
        "Cookie": "P_F=1; u_l_v_t=533; P_T=1571213753; __ysuid=15530827876500vD; cna=jRcZFaovJy0CAXd74ttufVC3; __ayft=1571192103618; __aysid=1571192103618dEq; __ayscnt=1; __arycid=dz-3-00; __arcms=dz-3-00; UM_distinctid=16dd283d870320-032d69a8089ec5-3d375b01-1fa400-16dd283d871c6a; juid=01dn987mgq1rie; ycid=0; _m_h5_tk=988bd010111d303724af98018d0f0889_1571209784007; _m_h5_tk_enc=48f54294a00ad6c257f7b8ead37479a9; CNZZDATA1277955961=1326476574-1571192884-https%253A%252F%252Fwww.baidu.com%252F%7C1571204594; seid=01dn9ir66r1ucc; yseid=1571206046475R0OABU; yseidcount=2; referhost=https%3A%2F%2Fwww.baidu.com; __arpvid=1571206488020WwkhPQ-1571206488035; __aypstp=5; __ayspstp=5; seidtimeout=1571208289969; ypvid=1571206492611ZX67xa; ysestep=3; yseidtimeout=1571213692612; ystep=4; __ayvstp=19; __aysvstp=19; isg=BE9Pm7JaTuCYYkuQk6FrWd3T3uOZ3Fi9jrh4SGFcgL7nMG8yaUSg5qQgNiArSHsO"}
    try:
        res = requests.get(url, headers=request_headers)
        res.raise_for_status()
        vid = res.text[res.text.index("{"):-1]
        json_str = json.loads(vid)
        # print(json_str)
        vid_datas = json_str['data']
        videos = vid_datas['data']
        vid_list = videos["videos"]
        vid_list = vid_list["list"]
        # print(vid_list)
        return vid_list
    except:
        print("爬取页码失败")
        print(res.text)
    return None


'''批量爬取'''


def batch_spilder():
    vid_list = yk_vid_spilder()
    if vid_list is None:
        print("获取页码失败，无法爬取")
        return
    for vids in vid_list:
        print('爬取电视剧[' + str(vids["title"]) + ']的弹幕')
        i = 0
        while True:
            time.sleep(random.randint(1, 2))
            print('开始爬取[' + str(vids["title"] + ']第' + str(i + 1) + '页'))
            # print(vids["vid"])
            comment = yk_comment_spilder(i, vids["vid"])
            i = i + 1
            if str_to_json(comment) == 0:
                break


'''
转为json
提取评论内容 
写入文件
'''


def str_to_json(comment):
    if comment is None:
        print(comment)
        return 0
    json_str = json.loads(comment)
    if 'code' in json_str.keys() and json_str['code'] == -1:
        return 0
    print("爬取弹幕[" + str(json_str['count']) + "]条")
    if json_str['count'] == 0:
        return 0
    comments = json_str['result']
    with open('comments.txt', 'a+', encoding='utf-8') as file:
        for c in comments:
            # print(c['content'])
            file.write(c['content'])
    return 1


if __name__ == '__main__':
    batch_spilder()
